<?php
// +------------------------------------------------------------
// | Name: 星空框架 仓库模型管理
// +------------------------------------------------------------
// | Version: V5.0
// +------------------------------------------------------------
// | Author: xkyinzhe <zhonghuaxinxing@sina.cn>
// +------------------------------------------------------------
// | Time: 2021-04-25 14:36
// +------------------------------------------------------------
declare(strict_types=1);

namespace app\common\model;

use think\model\concern\SoftDelete;
use app\common\model\ShippingTemplate as ShippingTemplateModel;
/**
 * @xk 仓库模型管理
 */
class Store extends BaseModel
{

  use SoftDelete;
  protected $name = 'store';
  protected $pk = 'store_id';
  protected $deleteTime = 'delete_time';
  /**
   * @xk 写入前操作
   *
   * @method onBeforeWrite
   * @param  [type]                  $data                     [description]
   * @return [type]                                            [description]
   * @Author xkyinzhe                <zhonghuaxinxing@sina.cn>
   * @Time   2021-04-27T09:17:57+080
   */
  public static function onBeforeWrite($data)
  {
      unset($data['act']);
      unset($data['controllers']);
      unset($data['actions']);
      if (!$data->create_time) {
          $data->update_time = $data->create_time = date('Y-m-d H:i:s');
      }
      if($data->order_id <= 0){
        $data->order_id = self::max('order_id')+1;
      }
  }
  /**
   * @xk 删除后操作
   *
   * @method onAfterDelete
   * @param  [type]        $data                      [description]
   * @return [type]        [description]
   * @Time   2021-10-18
   * @City   青岛
   * @Author xkyinzhe      <zhonghuaxinxing@sina.cn>
   */
  public static function onAfterDelete($data)
  {
      $shippingTemplateModel = new ShippingTemplateModel();
      $store_id = $data['store_id'];
      $condition = [];
      $condition[] =['store_id','=',$store_id];
      // 删除商品规格价格体系
      $shipping_template_res = $shippingTemplateModel->destroy(function($query) use ($condition){
        $query->where($condition);
      });
  }
}
